/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view.layer;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JToggleButton;

/**
 * Represents a boolean value which can be changed with a JCheckBox and a
 * JCheckBoxMenuItem.
 * 
 */
public class LayerBoolean extends AbstractAction {

    private static final long serialVersionUID = -1548134224894699360L;

    /** The value of this bool. */
    private boolean value;

    /** the checkbox for this value. */
    private JCheckBox checkBox;

    /** the menuItem for this value. */
    private JCheckBoxMenuItem menuItem;

    /** the toggle button for this value. */
    private JToggleButton toggleButton;

    /** The observers of this bool. */
    private Vector <LayerBooleanObserver> observers;
    
    /** flag whether the boolean value is changing or not. */
    private boolean isChanging;
    
    /**
     * Creates a new boolean with Checkbox and MenuItem.
     * 
     * @param name
     *            the name of the boolean.
     * @param initalValue
     *            the inital value.
     */
    public LayerBoolean(String name, boolean initalValue) {
        super(name);
        value = initalValue;
        observers = new Vector <LayerBooleanObserver> ();
        checkBox = new JCheckBox(name, value);
        checkBox.addActionListener(this);
        menuItem = new JCheckBoxMenuItem(name, value);
        menuItem.addActionListener(this);
        toggleButton = new JToggleButton(name, value);
        toggleButton.addActionListener(this);
    }

    /**
     * if the value should change.
     */
    public void actionPerformed(ActionEvent e) {
        setSelected(!value);
    }

    /**
     * returns the value.
     * 
     * @return the value.
     */
    public boolean getValue() {
        return value;
    }

    /**
     * Changes the selection state.
     * 
     * @param newValue
     *            new selection state - the new value
     */
    public void setSelected(boolean newValue) {
        value = newValue;
        checkBox.setSelected(value);
        menuItem.setSelected(value);
        toggleButton.setSelected(value);
        
        if (isChanging) {
            return;
        }
        isChanging = true;
        Iterator <LayerBooleanObserver> it = observers.iterator();
        while (it.hasNext()) {
            it.next().boolChanged();
        }
        isChanging = false;
    }

    /**
     * return the checkBox.
     * 
     * @return the checkBox
     */
    public JCheckBox getCheckBox() {
        return checkBox;
    }

    /**
     * returns the menu item.
     * 
     * @return the menu item
     */
    public JCheckBoxMenuItem getMenuItem() {
        return menuItem;
    }

    /**
     * Returns the toggle button.
     * 
     * @return the toggle button
     */
    public JToggleButton getToggleButton() {
        return toggleButton;
    }
    
    /**
     * Adds a observer to the layerBoolean.
     * @param observer the new observer.
     */
    public void addObserver(LayerBooleanObserver observer) {
        if (!observers.contains(observer)) {
            observers.add(observer);
        }
    }
    
    /**
     * Removes a observer from this layerBoolean.
     * @param observer the observer to remove
     */
    public void removeObserver(LayerBooleanObserver observer) {
        observers.remove(observer);
    }
}
